/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package APP3.services;

import APP3.bookshop.book.ABook;
import APP3.bookshop.main.BookShop;
import APP3.bookshop.person.author.Author;
import javax.swing.DefaultListModel;

/**
 *
 * @author Asus
 */
public class AuthorService {

    private BookShop bookShop;

    public AuthorService(BookShop bookShop) {
        this.bookShop = bookShop;
    }

    public DefaultListModel populateAuthorList() {
        DefaultListModel authList = new DefaultListModel();
        for (Author auth : bookShop.getAuthors()) {
            authList.addElement(auth);

        }

        return authList;

    }

    public DefaultListModel populateAuthorsBooksList(Author a) {
        DefaultListModel authBooksList = new DefaultListModel();
        for (ABook book : a.getBooks()) {
            authBooksList.addElement(book);

        }

        return authBooksList;

    }

    public void removeAuthor(Author a) {
        //remove all the related books as well
        bookShop.getAuthors().remove(a);
    }

    public String addAuthor(String name) {
        if (name.equals("")) {
            return "INPUT ERROR";
        }
        Author a = new Author();
        a.setName(name);
        
        bookShop.getAuthors().add(a);

        return "SAVED";
    }

}
